package com.mzm.util.persistency;

import java.util.List;

import javax.persistence.Query;

import org.springframework.orm.jpa.JpaTemplate;

/**
 * 
 * @author daniel
 *
 */
public class JpqlQueryBuilder extends QueryBuilder {

	protected Class<?> selectedEntity;

	protected JpaTemplate jpaTemplate;

	public JpqlQueryBuilder() {
	}

	public JpqlQueryBuilder(final JpaTemplate jpaTemplate) {
		this.jpaTemplate = jpaTemplate;
	}

	public JpqlQueryBuilder(final Class<?> selectedEntity) {
		this.selectedEntity = selectedEntity;
	}

	public void fillQueryParameters(final Query query) {
		Object[] queryParams = getQueryParams();
		for (int i = 0; i < queryParams.length; i++) {
			query.setParameter(i + 1, queryParams[i]);
		}
	}

	public <T> List<T> find(){
		return jpaTemplate.find(this.getQueryString(),this.getQueryParams());
	}
}
